<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Paginator;

class Operation extends Controller
{
	
	public function operation()
	{
		return view();
	}
	public function operation_json()
	{
		$page = input('get.page');
		$limit = input('get.limit');
		$res = DB::table('student')->page($page)->limit($limit)->select();
		$count = count(Db::table('student')->select());
		$arr_res=array(
			'code'	=>	0,
			'msg' 	=>	'',
			'count'	=>	$count,
			'data' 	=>	$res
		);
		$json_data=json_encode($arr_res);
		echo $json_data;
	}
	public function search_stuId()
	{
		$stuId = input('get.stuId');
		$page = input('get.page');
		$limit = input('get.limit');
		$res = Db::table('student')->where('stuId',$stuId)->page($page)->limit($limit)->select();
		$count = count($res);
		$arr_res=array(
			'code'	=>	0,
			'msg' 	=>	'',
			'count'	=>	$count,
			'data' 	=>	$res
		);
		$json_data=json_encode($arr_res);
		echo $json_data;
	}
	public function del_stu()
	{
		$stuId = input('post.stuId');
		$res = Db::table('student')->where('stuId',$stuId)->delete();
		$ress = Db::table('student')->where('stuId','gt',$stuId)->setDec('stuId');
		if ($res) {
			$arr_data=array(
				'msg' 	=> 	'成功',
				'icon'	=>	'1'
			);
			$json_data = json_encode($arr_data);
			echo $json_data;
		}else{
			$arr_data=array(
				'msg' 	=> 	'失败',
				'icon'	=>	'2'
			);
			$json_data = json_encode($arr_data);
			echo $json_data;
		}
	}
	public function updata_stu()
	{
		$stuId = input('post.stuId');
		$field = input('post.field');
		$value = input('post.value');
		$res = Db::table('student')->where('stuId',$stuId)->setField($field,$value);
		if ($res) {
			$arr_data=array(
				'msg' 	=> 	'成功',
				'icon'	=>	'1'
			);
			$json_data = json_encode($arr_data);
			echo $json_data;
		}else{
			$arr_data=array(
				'msg' 	=> 	'失败',
				'icon'	=>	'2'
			);
			$json_data = json_encode($arr_data);
			echo $json_data;
		}
	}
	public function add_sut()
	{
			$data = input('post.');
		$arr = array();
		for($i=0;$i<count($data['stuName']);$i++){
			$arr['stuName'] = $data['stuName'][$i]; 
			$arr['gender'] = $data['gender'][$i]; 
			$arr['age'] = $data['age'][$i]; 
			$arr['height'] = $data['height'][$i]; 
			$arr['weight'] = $data['weight'][$i]; 
			$res = Db::table('student')->insert($arr);
		}


		if ($res) {
			$this->success('添加成功！','Operation/operation','1');
		}else{
			$this->error('添加失败');
		};
		
	}
}
